// pages/outcome/outcome.js
const app = getApp()
import utils from "../../utils/util"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBarData: app.globalData.navBarData,
        type: "exercise",
        outcomeData: null,
        history: null
    },

    problemHandler: function (event) {
        let problemnum = event.currentTarget.dataset.id
        let lunchData = {
            type: 'review',
            // TODO problemnum 应该标准化
            problemnum: problemnum
        }
        app.globalData.problemLunchData = lunchData
        wx.navigateTo({
            url: '/pages/problem/problem',
        })
    },
    navBack() {
        wx.navigateBack({
            delta: 1,
        }).catch(function () {
            wx.switchTab({
                url: '/pages/exam/exam',
            })
        })
    },


    /**
     * 计算正确率等信息并返回
     */
    countOutComeData: function (history) {
        let right = 0
        let total = history.length
        history.forEach(element => {
            if (element.correct) {
                right++
            }
        });
        return {
            right: right,
            total: total,
            percent: (right / total * 100).toFixed(2)
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        options = JSON.parse(options.data)
        let type = options.type
        let that = this
        let history, outcomeData

        if (type == 'exercise') {
            history = app.globalData.answerHistory
            outcomeData = this.countOutComeData(history)
            let tag = wx.getStorageSync('exerciseTag')
            history.sort(function (a, b) {
                return a.problemnum - b.problemnum
            })
            outcomeData.tags = tag
            this.setData({
                type: type,
                outcomeData: outcomeData,
                history: history
            })
        } else if (type == 'contest') {
            this.setData({
                type: type
            })
            // 比赛展示数据初始化

            let api = utils.APIS.getContestLeaderboard
            let data = {
                contestid: options.contestid
            }
            let registered = options.registered
            wx.showLoading({
                title: '请稍后',
            })
            utils.request({
                ...api,
                data: data
            }).then(function (res) {
                wx.hideLoading().catch((e) => {})
                if (res.data.ret == 0) {
                    //TODO leadboar等信息
                    outcomeData = {
                        ...res.data,
                        contestname: options.contestname
                    }
                    delete outcomeData.ret
                    that.setData({
                        outcomeData: outcomeData
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "error"
                    })
                }
            }).catch(function (e) {
                wx.hideLoading().catch((e) => {})
            })
            if (!registered) {
                this.setData({
                    noProblem: true
                })
            } else {
                //TODO 答题信息
                wx.showLoading({
                    title: '请稍后',
                })
                let api = utils.APIS.getContestRecord
                utils.request({
                    ...api,
                    data: data
                }).then((res) => {
                    wx.hideLoading().catch((e) => {})
                    if (res.data.ret == 0) {
                        history = res.data.items
                        app.globalData.answerHistory = history
                        that.setData({
                            history: history
                        })
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: "error"
                        })
                    }
                }).catch((e) => {
                    wx.hideLoading().catch((e) => {})
                })
            }

        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        utils.initGlobalData(this)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})